package com.cnsugar.ai.face;

import com.cnsugar.ai.face.bean.FaceIndex;
import com.cnsugar.ai.face.dao.FaceDao;

import java.util.List;

/**
 * @Author ChenZhangKun
 * @Date 2020/4/24 11:09
 */
@SuppressWarnings("all")
public class LoadFace  {
    public void run() {
        if (SeetafaceBuilder.face_db_status != SeetafaceBuilder.FacedbStatus.READY) {
            return;
        }
        if (System.getProperty("seetaface.db") == null) {
            SeetafaceBuilder.face_db_status = SeetafaceBuilder.FacedbStatus.INACTIV;
            SeetafaceBuilder.logger.error("没有配置sqlite.db.file!!!");
            return;
        }
        SeetafaceBuilder.face_db_status = SeetafaceBuilder.FacedbStatus.LOADING;
        SeetafaceBuilder.logger.info("load face data...");
        FaceDao.clearIndex();
        int pageNo = 0, pageSize = 100;
        while (true) {
            List<FaceIndex> list = FaceDao.findFaceImgs(pageNo, pageSize); // face_image表中获取
            if (list == null) {
                break;
            }
            for (FaceIndex faceIndex : list) {
                synchronized (LoadFace.class) {
                    SeetafaceBuilder.logger.info(Thread.currentThread().getName() + "  开始加载");
                    SeetafaceBuilder.register(faceIndex.getKey(), faceIndex);
                }
            }
            if (list.size() < pageSize) {
                break;
            }
            pageNo++;
        }
        SeetafaceBuilder.face_db_status = SeetafaceBuilder.FacedbStatus.OK;
    }
}
